import request from '@/utils/request'

export const getChannels = () => {
  return async dispatch => {
    const data = await request.get('channels')
    dispatch({ type: 'article/getChannels', payload: data.channels })
  }
}

export const getArticles = (params) => {
  return async dispatch => {
    const data = await request.get('mp/articles', { params })
    dispatch({ type: 'article/getArticles', payload: data })
  }
}

export const delArticle = id => {
  return async dispatch => {
    await request.delete('mp/articles/' + id)
  }
}

export const addArticle = (data) => {
  return async dispatch => {
    await request.post(`mp/articles?draft=false`, data)
  }
}

export const editArticle = (data) => {
  return async dispatch => {
    await request.put(`mp/articles/${data.id}?draft=false`, data)
  }
}

export const getArticle = id => {
  return async dispatch => {
    const data = await request.get('mp/articles/' + id)
    return data
  }
}